package com.wsfk.ssm.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipUtil;
import org.springframework.stereotype.Service;

import com.wsfk.ssm.entity.HandBook;
import com.wsfk.ssm.entity.Picture;
import com.wsfk.ssm.entity.TempImage;
import com.wsfk.ssm.entity.User;
import com.wsfk.ssm.messages.CodeMessage;
import com.wsfk.ssm.path.CallPath;
import com.wsfk.ssm.path.POPS;
import com.wsfk.ssm.tools.CompressImgUtil;
import com.wsfk.ssm.tools.QRCodeUtil;
@Service("FileSvcImpl")
public class FileSvcImpl implements FileSvc {

	private POPS pops;

	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.FileSvc#saveBook(com.wsfk.ssm.entity.User, com.wsfk.ssm.entity.HandBook, java.io.File, java.lang.String)
	 */
	@Override
	public boolean saveBook(User user, HandBook book, File file,String model)
			throws IOException {

		CallPath cp = new CallPath().savePre(pops).iconWay(user, book);
		String imagepath = cp.build();
		FileUtils.copyFile(file, new File(imagepath));
		
		CompressImgUtil ciu =new CompressImgUtil();
		ciu.compressPic(file, imagepath,119, 159, true);
		
		CallPath c = new CallPath().savePre(pops).bookWay(user, book);
		String bookPath = c.build();
		
		File srcFile =new File(model);
		File destDir = new File(bookPath);
		FileUtils.copyDirectory(srcFile, destDir);
		
		return true;
	}

	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.FileSvc#saveContent(com.wsfk.ssm.entity.User, com.wsfk.ssm.entity.HandBook, java.util.List)
	 */
	@Override
	public boolean saveContent(File file,Picture picture) throws IOException {
		CallPath cp = new CallPath().savePre(pops).picWay(picture);
		String path = cp.build();
		FileUtils.copyFile(file, new File(path));
		return true;
	}
	@Override
	public boolean saveContent(User user, HandBook book, List<File> uploadPic,
			List<String> place) throws IOException {
		CallPath cp = new CallPath().savePre(pops).imageWay(user, book);
		List<String> paths = cp.builds(place);
		
		for(int i=0;i<paths.size();i++){
			if(i<uploadPic.size()){
				FileUtils.copyFile(uploadPic.get(i), new File(paths.get(i)));				
			}
		}
		return true;
		
	}

	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.FileSvc#saveZip(com.wsfk.ssm.entity.User, com.wsfk.ssm.entity.HandBook)
	 */
	@Override
	public boolean saveZip(User user, HandBook book) {

		String path = pops.getSavePath() + "/" + book.getBookdir();
		CallPath cp = new CallPath().savePre(pops).zipWay(user, book);
		Project prj = new Project();
		Zip zip = new Zip();
		zip.setProject(prj);
		zip.setDestFile(new File(cp.build()));
		FileSet fileSet = new FileSet();
		fileSet.setProject(prj);
		fileSet.setDir(new File(path));
		fileSet.setExcludes("**/*.zip"); // 排除哪些文件或文件夹
		zip.addFileset(fileSet);
		zip.execute();
		return true;
	}

	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.FileSvc#saveTowCodes(com.wsfk.ssm.entity.User, com.wsfk.ssm.entity.HandBook, java.lang.String)
	 */
	@Override
	public boolean saveTowCodes(User user, HandBook book, String code) {
		CallPath cp = new CallPath().savePre(pops).codeWay(user, book);
		String path = cp.build();
		CodeMessage cm =new CodeMessage(code);
		QRCodeUtil.createQRcode(path, cm);
		return true;
	}


	@Resource
	public void setPops(POPS pops) {
		this.pops = pops;

	}

	@Override
	public boolean saveImage(String userId, String bookId, String imageId,
			File file) throws IOException {
		//临时文件存储
		String imagePath = new CallPath().savePre(pops)
				.readyImageWay(userId, bookId, imageId).build();
		FileUtils.copyFile(file, new File(imagePath));
		return true;
	}

	@Override
	public boolean savePic(User user, HandBook handBook, TempImage tempImage,
			String no) throws IOException {
		// PIC文件存储
		String imagePath = new CallPath().savePre(pops)
				.readyImageWay(user.getId(), handBook.getId(), tempImage.getId()).build();
		String picPath = new CallPath().savePre(pops).clearUpImageWay(user, handBook, no).build();
		
		FileUtils.copyFile(new File(imagePath), new File(picPath));
		return true;
	}
	@Override
	public boolean updateIcon(Picture picture, File file) throws IOException {
		
		CallPath cp = new CallPath().savePre(pops).picWay(picture);
		String path = cp.build();
		
		FileUtils.copyFile(file, new File(path));
		CompressImgUtil ciu =new CompressImgUtil();
		ciu.compressPic(file, path,119, 159, true);
		
		return true;
		
	}



	

}
